package com.springdoor.web.controller.goods;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springdoor.common.annotation.Log;
import com.springdoor.common.core.controller.BaseController;
import com.springdoor.common.core.domain.AjaxResult;
import com.springdoor.common.enums.BusinessType;
import com.springdoor.common.core.domain.entity.SprdGoodsBrand;
import com.springdoor.goods.service.ISprdGoodsBrandService;
import com.springdoor.common.utils.poi.ExcelUtil;

/**
 * 商品品牌Controller
 * 
 * @author llshs
 * @date 2024-04-30
 */
@RestController
@RequestMapping("/goods/brand")
public class SprdGoodsBrandController extends BaseController
{
    @Autowired
    private ISprdGoodsBrandService brandService;

    /**
     * 查询商品品牌列表
     */
    @PreAuthorize("@ss.hasPermi('goods:brand:list')")
    @GetMapping("/list")
    public AjaxResult list(SprdGoodsBrand sprdGoodsBrand)
    {
        List<SprdGoodsBrand> list = brandService.selectSprdGoodsBrandList(sprdGoodsBrand);
        return success(list);
    }

    /**
     * 导出商品品牌列表
     */
    @PreAuthorize("@ss.hasPermi('goods:brand:export')")
    @Log(title = "商品品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SprdGoodsBrand sprdGoodsBrand)
    {
        List<SprdGoodsBrand> list = brandService.selectSprdGoodsBrandList(sprdGoodsBrand);
        ExcelUtil<SprdGoodsBrand> util = new ExcelUtil<SprdGoodsBrand>(SprdGoodsBrand.class);
        util.exportExcel(response, list, "商品品牌数据");
    }

    /**
     * 获取商品品牌详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:brand:query')")
    @GetMapping(value = "/{brandId}")
    public AjaxResult getInfo(@PathVariable("brandId") Long brandId)
    {
        return success(brandService.selectSprdGoodsBrandByBrandId(brandId));
    }

    /**
     * 新增商品品牌
     */
    @PreAuthorize("@ss.hasPermi('goods:brand:add')")
    @Log(title = "商品品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SprdGoodsBrand brand)
    {
        if (!brandService.checkBrandCodeUnique(brand))
        {
            return error("新增品牌失败，品牌编码 " + brand.getBrandCode() + " 已存在");
        }
        if (!brandService.checkBrandNameUnique(brand))
        {
            return error("新增品牌失败，同组品牌名称 " + brand.getBrandName() + " 已存在");
        }
        brand.setCreateBy(getUsername());
        brand.setUpdateBy(getUsername());
        return toAjax(brandService.insertSprdGoodsBrand(brand));
    }

    /**
     * 修改商品品牌
     */
    @PreAuthorize("@ss.hasPermi('goods:brand:edit')")
    @Log(title = "商品品牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SprdGoodsBrand brand)
    {
        if (!brandService.checkBrandCodeUnique(brand))
        {
            return error("修改品牌失败，品牌编码 " + brand.getBrandCode() + " 已存在");
        }
        if (!brandService.checkBrandNameUnique(brand))
        {
            return error("修改品牌失败，同组品牌名称 " + brand.getBrandName() + " 已存在");
        }
        else if (brand.getParentId().equals(brand.getBrandId()))
        {
            return error("修改品牌信息失败，上级部门不能是自己");
        }
        brand.setUpdateBy(getUsername());
        return toAjax(brandService.updateSprdGoodsBrand(brand));
    }

    /**
     * 删除商品品牌
     */
    @PreAuthorize("@ss.hasPermi('goods:brand:remove')")
    @Log(title = "商品品牌", businessType = BusinessType.DELETE)
	@DeleteMapping("/{brandIds}")
    public AjaxResult remove(@PathVariable Long[] brandIds)
    {
        return toAjax(brandService.deleteSprdGoodsBrandByBrandIds(brandIds));
    }

    @GetMapping("/optionSelect")
    public AjaxResult optionSelect() {
        return success(brandService.selectBrandAll());
    }

    @GetMapping("/treeSelect")
    public AjaxResult treeSelect() {
        List<SprdGoodsBrand> list = brandService.selectBrandAll();
        return success(brandService.buildAttrTreeSelect(list));
    }
}
